// QuestionsCollectedServiceImpl.java
package com.scuec.groupfive.service.impl;

import com.scuec.groupfive.entity.QuestionsCollectedEntity;
import com.scuec.groupfive.entity.User;
import com.scuec.groupfive.mapper.QuestionsCollectedMapper;
import com.scuec.groupfive.service.QuestionsCollectedService;
import com.scuec.groupfive.vo.QuestionsCollectedVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class QuestionsCollectedServiceImpl implements QuestionsCollectedService {

    @Autowired
    private QuestionsCollectedMapper questionsCollectedMapper;

    @Override
    public Map<String, Object> getCollectedQuestions(Integer userId, Integer subjectId, Integer pageNum, Integer pageSize) {
        Integer offset = (pageNum - 1) * pageSize;
        List<QuestionsCollectedEntity> entities = questionsCollectedMapper.getCollectedQuestions(
                userId,
                subjectId == null || subjectId == 0 ? null : subjectId,
                offset,
                pageSize
        );

        int total = questionsCollectedMapper.countCollectedQuestions(
                userId,
                subjectId == null || subjectId == 0 ? null : subjectId
        );

        List<QuestionsCollectedVO> list = new ArrayList<>();
        for (QuestionsCollectedEntity entity : entities) {
            QuestionsCollectedVO vo = new QuestionsCollectedVO();
            BeanUtils.copyProperties(entity, vo);
            list.add(vo);
        }

        Map<String, Object> result = new HashMap<>();
        result.put("total", total);
        result.put("list", list);
        return result;
    }

    @Override
    public User searchUser(String username) {
        return questionsCollectedMapper.searchUser(username);
    }
}